package com.yootk.config;

import com.yootk.interceptor.WebServiceAuthInterceptor;
import com.yootk.service.IMessageService;
import org.apache.cxf.Bus;
import org.apache.cxf.jaxws.EndpointImpl;
import org.apache.cxf.transport.servlet.CXFServlet;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.xml.ws.Endpoint;

/**
 * 服务注册配置类
 */
@Configuration
public class CXFConfig {

    @Autowired
    private Bus bus;    // Bus接口实例

    @Autowired
    private IMessageService messageService;     // 业务接口实例

    @Autowired
    private WebServiceAuthInterceptor interceptor;  // 服务端拦截器

    // 获取服务地址
    @Bean
    public ServletRegistrationBean getRegistrationBean() {
        return new ServletRegistrationBean(new CXFServlet(), "/services/*");    // 设置映射路径
    }

    // 配置终端
    @Bean
    public Endpoint messageEndPoint() {
        EndpointImpl endpoint = new EndpointImpl(this.bus, this.messageService);
        endpoint.publish("/MessageService");    // 服务终端
        endpoint.getInInterceptors().add(this.interceptor);     // 追加拦截器
        return endpoint;
    }
}
